package com.woniu113.junk.site.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.site.dto.SuggestionDTO;
import com.woniu113.junk.site.form.suggestion.AddSuggestionForm;
import com.woniu113.junk.site.form.suggestion.AllSuggestionForm;
import com.woniu113.junk.site.model.Suggestion;
import com.woniu113.junk.site.param.suggestion.AddSuggestionParam;
import com.woniu113.junk.site.param.suggestion.AllSuggestionParam;
import com.woniu113.junk.site.service.SuggestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/suggestion")
@Api(value = "SuggestionController", tags = "建议信息接口")
public class SuggestionController {

    @Resource
    private SuggestionService suggestionService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增建议信息
     * @param form 新增的建议数据
     * @param token 用于获取当前登录用户的id
     * @return 返回新增的建议数据成功提示和code
     * @throws Exception
     */
    @PostMapping("/addSuggestion")
    @ApiOperation(value = "用户提建议(新增)")
    private Result<Suggestion> addSuggestion(@RequestBody AddSuggestionForm form, @RequestHeader String token) throws Exception{
        System.out.println("建议内容"+form.getSuggestionContent());
        System.out.println("建议类型"+form.getUserType());
        //获取当前登录用户的id
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println("用户id"+userId);
        //执行新增
        Suggestion newSuggestion = suggestionService.addSuggestion(BeanUtil.toBean(form, AddSuggestionParam.class),userId);
        return Result.getResult(Suggestion.class).setData(newSuggestion).setMsg("新增建议成功").setCode(200);
    }

    @PostMapping("/findAll")
    @ApiOperation(value = "查询所有建议")
    private Result<List<Suggestion>> findAll(@RequestBody AllSuggestionForm form){
        System.out.println("分页数据1"+form.getPageNum());
        System.out.println("分页数据2"+form.getPageSize());
        System.out.println("建议类型"+form.getUserType());
        // 计算查询的起始索引
        int offset = (form.getPageNum() - 1) * form.getPageSize();
        SuggestionDTO dto = suggestionService.findAll(BeanUtil.toBean(form, AllSuggestionParam.class),offset);
        Integer total = dto.getTotal();
        return Result.getListResult(Suggestion.class).setData(dto.getList()).setMsg("查询全部建议成功").setCode(200).setTotal(dto.getTotal());
    }

    @GetMapping("/deleteSuggestion")
    @ApiOperation(value = "删除建议")
    private Result deleteSuggestion(@RequestParam("suggestionId") Integer suggestionId){
      suggestionService.removeById(suggestionId);
      return Result.getResult().setMsg("删除建议成功").setCode(200);
    }
}

